package com.itetchologyandenglish.home.controller.Api;

import com.alibaba.fastjson.annotation.JSONField;
import com.github.pagehelper.PageInfo;
import com.itetchologyandenglish.home.config.Log;
import com.itetchologyandenglish.home.enetity.AuthUser;
import com.itetchologyandenglish.home.enetity.Company;
import com.itetchologyandenglish.home.enetity.Result;
import com.itetchologyandenglish.home.service.CompanyService;
import com.itetchologyandenglish.home.service.CompanyTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Api(tags={"企业模块"})
@RestController
@RequestMapping("/company")
public class CompanyApiController {
    /**
     * 普通企业查询
     */
    @Resource
    CompanyService companyService;
    @Resource
    CompanyTypeService companyTypeService;
    @ApiOperation("查看企业列表")
    @Log("普通企业管理-根据条件企业列表")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss") //返回时间类型
    @DateTimeFormat(pattern = "yyyy-MM-dd") //接收时间类型
    @RequestMapping(value = "/normalCompanyList", produces = "application/json")
    public Result<PageInfo> normalCompanyList(@RequestParam("page") Integer page, @RequestParam("companyNames") String companyName, @RequestParam("companyProject") String companyProject, @RequestParam("searchbegin") String searchbegin, @RequestParam("searchend") String searchend, @RequestParam("limit") Integer limit) {
        System.out.println(companyName + " " + companyProject + " " + searchbegin + " " + searchend);
        PageInfo companyList = companyService.searchCompany(companyName, companyProject, searchbegin, searchend, limit, page, 0);
        return new Result<PageInfo>(200, "success", companyList);
    }
    @ApiOperation("删除企业")
    @Log("普通企业管理-根据ID删除企业")
    @RequestMapping(value = "/deleteNormalCompany", produces = "application/json")
    public Result deleteNormalCompany(@RequestParam("companyId") Integer companyId) {
        if (companyService.deleteNormalCompany(companyId)) {
            return new Result(200, "删除成功");
        }
        return new Result(200, "删除失败");
    }
    @ApiOperation("查询企业")
    @Log("普通企业管理-根据ID查询企业")
    @RequestMapping(value = "/findNormaoCompanyById", produces = "application/json")
    public Result<Company> findNormaoCompanyById(@RequestParam("companyId") Integer companyId) {
        Company company = companyService.findNormaoCompanyById(companyId);
        System.out.println(company);
        if (company != null) {
            return new Result(200, "success", company);
        }
        return new Result(200, "没有该信息");
    }
    @ApiOperation("更新企业")
    @Log("普通企业管理-根据企业信息等更新企业")
    @RequestMapping(value = "/updateNormalcompany", produces = "application/json", method = RequestMethod.POST)
    public Result updateNormalcompany(HttpServletRequest request) {
//        (@RequestParam CommonsMultipartFile  cardPic
        int companyId = Integer.parseInt(request.getParameter("comid"));
        String companyName = request.getParameter("comName");
        String liaison = request.getParameter("contacts");
        String tel = request.getParameter("telephone");
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String companyProject = request.getParameter("management");
        String time = request.getParameter("registerTime");
        Date createTime = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            createTime = dateFormat.parse(time);
            System.out.println(createTime.toString().split(" ")[0]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Company company = new Company().setCompanyId(companyId).setCompanyName(companyName)
                .setLiaison(liaison).setTel(tel).setUserName(userName)
                .setPassword(password)
                .setCompanyProject(companyProject)
                .setCreateTime(createTime);
        System.out.println(company);
        if (companyService.updateNormalcompany(company)) {
            return new Result(200, "修改成功");
        } else {
            return new Result(200, "修改失败");
        }
    }

    Date format(String dates) {
        Date date = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = dateFormat.parse(dates);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //return date;
        return date;
    }
    @ApiOperation("升级VIP")
    @Log("普通企业管理-根据企业ID升级成VIP")
    @RequestMapping(value = "/upgradetovip", produces = "application/json", method = RequestMethod.POST)
    public Result upgradetovip(Integer companyid, String vipbegin, String vipend) {
        //前-后 controller
        //@DateTimeFormat(pattern="yyyy-MM-dd")Date date
        //后-前
        //@JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
        Date vipbegins = format(vipbegin);
        Date vipends = format(vipend);
        System.out.println(vipbegins + ":" + vipends);
        if (companyService.upgradetovip(companyid, vipbegins, vipends)) {
            return new Result(200, "升级成功");
        } else {
            return new Result(200, "升级失败");
        }
    }
    @ApiOperation("查看VIP企业列表")
    @Log("VIP企业管理-根据条件查看VIP企业列表")
    @RequestMapping(value = "/vipcomlist", produces = "application/json")
    public Result<PageInfo> vipcomlist(@RequestParam("page") Integer page, @RequestParam("companyNames") String companyName, @RequestParam("companyProject") String companyProject, @RequestParam("searchbegin") String searchbegin, @RequestParam("searchend") String searchend, @RequestParam("limit") Integer limit) {
        System.out.println(companyName + " " + companyProject + " " + searchbegin + " " + searchend);
        System.out.println(page);
        PageInfo companyList = companyService.searchCompany(companyName, companyProject, searchbegin, searchend, limit, page, 1);
        return new Result<PageInfo>(200, "success", companyList);
    }
    @ApiOperation("添加VIP企业")
    @Log("VIP企业管理-新增VIP企业")
    @RequestMapping(value = "/vipcomsave", produces = "application/json")
    public Result vipComsave(@RequestParam(value = "files", required = false) CommonsMultipartFile cardPic, Company company, HttpServletRequest request, HttpSession session) throws IOException {
        company.setCompanyType(companyTypeService.getCompanyById(company.getCompanyTypeId()));
//        String path = request.getServletContext().getRealPath("WEB-INF/static/images/company");
//        String path ="C:\\lc_java_idea\\SSMBS\\src\\main\\webapp\\WEB-INF\\static\\images\\company\\";
        //  String pathS="C:\\lc_java_idea\\SSMBS\\target\\SSMBS\\WEB-INF\\static\\images\\";
//      r("/companyPic/**").("file:C:\\lc_java_idea\\SSMBS\\target\\SSMBS\\WEB-INF\\static\\images\\");
        String path = "C:\\lc_java_idea\\SSMBS\\src\\main\\webapp\\WEB-INF\\static\\images\\company\\";
        System.out.println(cardPic.getOriginalFilename());
        String uuid = UUID.randomUUID().toString();
        String fileName = uuid + "-" + cardPic.getOriginalFilename();
        File file = new File(path, fileName);
        cardPic.transferTo(file);
        AuthUser user = (AuthUser) session.getAttribute("user");
        company.setAuthUser(user);
        //可以改进成动态，暂时不修改
        //company.setCompanyPicUrl("http://localhost:8080/SSMBS/static/images/company/" + cardPic.getOriginalFilename());
        company.setCompanyPicUrl("images/company/" + fileName);
        System.out.println(company);
        if (companyService.insertVipCompany(company)) {
            return new Result(200, "添加成功");
        } else {
            return new Result(200, "添加失败");
        }
    }
    @ApiOperation("修改VIP企业信息")
    @Log("VIP企业管理-根据VIP企业信息修改VIP")
    @RequestMapping(value = "/updateVipCompany", produces = "application/json")
    public Result updateVipCompany(@RequestParam("files") CommonsMultipartFile cardPic, Company company, HttpServletRequest request, HttpSession session) throws IOException {
        company.setCompanyType(companyTypeService.getCompanyById(company.getCompanyTypeId()));
        System.out.println(company);
        String path = "C:\\lc_java_idea\\SSMBS\\src\\main\\webapp\\WEB-INF\\static\\images\\company\\";
        //  String path = request.getServletContext().getRealPath("WEB-INF/static/images");
        String uuid = UUID.randomUUID().toString();
        String fileName = uuid + "-" + cardPic.getOriginalFilename();
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();
        }
        if (cardPic.getOriginalFilename().equals("") || cardPic.getOriginalFilename() == null) {
            String companyPicUrl = companyService.findNormaoCompanyById(company.getCompanyId()).getCompanyPicUrl();
            company.setCompanyPicUrl(companyPicUrl);
            System.out.println(companyPicUrl.substring(companyPicUrl.lastIndexOf('/') + 1));
        } else {
            File file = new File(path, fileName);
            if (!file.exists()) {
                cardPic.transferTo(file);
            }
            company.setCompanyPicUrl("images/company/" + fileName);
        }
        System.out.println(company);
        AuthUser user = (AuthUser) session.getAttribute("user");
        company.setAuthUser(user);
        System.out.println(company);
        if (companyService.updateVipCompany(company)) {
            return new Result(200, "更新成功");
        } else {
            return new Result(200, "更新失败");
        }
    }
    @ApiIgnore
    @Log("VIP企业管理-检查VIP企业公司名")
    @RequestMapping(value = "/findVipCompanyByName", produces = "application/json")
    public Result<Company> findVipCompanyByName(@RequestParam("companyName") String companyName) {
        Company company = companyService.findVipCompanyByCompanyName(companyName);
        System.out.println(company);
        if (company == null) {
            return new Result<>(200, "success", null);
        } else {
            return new Result<Company>(200, "error", company);
        }

    }
    @ApiIgnore
    @Log("VIP企业管理-检查VIP企业用户名")
    @RequestMapping(value = "/findVipCompanyByUserName", produces = "application/json")
    public Result<List<Company>> findVipCompanyByUserName(@RequestParam("userName") String userName) {
        List<Company> company = companyService.findVipCompanyByUserName(userName);
        if (company == null) {
            return new Result<>(200, "success", null);
        } else {
            return new Result<List<Company>>(200, "error", company);
        }
    }

}
